﻿#include "StepEditDialog.h"
#include "DataTrans.h"
#include <QMessageBox>
#include "BTEProject.h"


class TimeValidator : public QValidator
{
public:
	TimeValidator(QObject *parent)
		:QValidator(parent)
	{
	}

	virtual State validate(QString &input, int &pos) const
	{
		QStringList list = input.split(":");
		if(list.size() == 4 && list[1].toInt() < 60 && list[2].toInt() < 60)
			return State::Acceptable;

		return State::Invalid;
	}
};

StepEditDialog::StepEditDialog(QWidget *parent)
	: QDialog(parent)
{
	ui.setupUi(this);	

	m_typeMap[0] = STEP_TYPE_7;
	m_typeMap[1] = STEP_TYPE_3;
	m_typeMap[2] = STEP_TYPE_1;
	m_typeMap[3] = STEP_TYPE_2;
	m_typeMap[4] = STEP_TYPE_IDLE;
	m_typeMap[5] = STEP_TYPE_LOOP;

	for(auto var: m_typeMap)
		ui.boxType->addItem(PackDefine::StepNameList[var]);	

	m_paramWidgets << ui.boxSetTime << ui.lineTime
		<< ui.label2 << ui.lineVolt << ui.label3 << ui.lineCurrent
		<< ui.label4 << ui.lineStopCurrent << ui.label5 << ui.lineStopVoltage
		<< ui.label6 << ui.lineStartStep << ui.label7 << ui.lineLoopTime;

	ui.lineTime->setInputMask("99:99:99:999");
	ui.lineTime->setText("00:00:00:000");

	auto *v = new TimeValidator(this);
	ui.lineTime->setValidator(v);
}

StepEditDialog::~StepEditDialog()
{
}

void StepEditDialog::showEvent(QShowEvent *event)
{
	stepToUi();
	on_boxType_currentIndexChanged(ui.boxType->currentIndex());

	__super::showEvent(event);
}

bool StepEditDialog::isVaildParam(int stepType, int param)
{
	//电流
	if (stepType == STEP_TYPE_1
		|| stepType == STEP_TYPE_2
		|| stepType == STEP_TYPE_7)
	{
		if (param == PARAM_Current)
			return true;
	}
	//电压
	if (stepType == STEP_TYPE_3
		|| stepType == STEP_TYPE_7)
	{
		if (param == PARAM_Voltage)
			return true;
	}
	//截止电流
	if (stepType == STEP_TYPE_3
		|| stepType == STEP_TYPE_7)
	{
		if (param == PARAM_StopCurrent)
			return true;
	}
	//截止电压
	if (stepType == STEP_TYPE_2
		|| stepType == STEP_TYPE_1)
	{
		if (param == PARAM_StopVoltage)
			return true;
	}
	//时间
	if (stepType == STEP_TYPE_1
		|| stepType == STEP_TYPE_2
		|| stepType == STEP_TYPE_3
		|| stepType == STEP_TYPE_IDLE
		|| stepType == STEP_TYPE_7)
	{
		if (param == PARAM_Time)
			return true;
	}
	//起始工步，循环次数
	if (stepType == STEP_TYPE_LOOP)
	{
		if (param == PARAM_CountLoop
			|| param == PARAM_BeginStepID)
			return true;
	}
	
	return false;
}

bool StepEditDialog::checkData(QString param_name, QString var_str, double max, double &out)
{
	bool ok;
	double var = var_str.toDouble(&ok);
	if (var_str.isEmpty())
	{
		QMessageBox::information(this, "", param_name + tr(" 不能为空"));
		return false;
	}

	if (!ok)
	{
		QMessageBox::information(this, "", param_name + tr("不正确，请检查"));
		return false;
	}

	if(var <= 0 || var > max)
	{
		QString range = QString::asprintf("(0-%0.4f)", max);	
		QMessageBox::information(this, "", param_name + tr("参数超出范围") + range + tr("，请检查"));
		return false;
	}

	out = var;
	return true;
}

void StepEditDialog::stepToUi()
{	
	int prec = 4;

	ui.lineNum->setText(QString::number(step.StepLine));
	ui.boxType->setCurrentIndex(m_typeMap.key(step.StepType, -1));
	
	if(step.isTimeVaild())
	{		
		ui.lineTime->setText(DataTrans::fromTimeUnit(step.Time()));
		ui.boxSetTime->setChecked(true);
	}
	
	if(step.isVoltageVaild())
		ui.lineVolt->setText(QString::number(step.Voltage(), 'f',prec));
	if(step.isCurrentVaild())
		ui.lineCurrent->setText(QString::number(step.Current(), 'f', prec));	
	if(step.isStopCurrentVaild())
		ui.lineStopCurrent->setText(QString::number(step.StopCurrent(), 'f', prec));
	if (step.isStopVoltageVaild())
		ui.lineStopVoltage->setText(QString::number(step.StopVoltage(), 'f', prec));
}

bool StepEditDialog::uiToStep()
{	
	QString lineVolt	= ui.lineVolt->text().trimmed();
	QString lineCurrent = ui.lineCurrent->text().trimmed();	
	QString lineStopCurrent = ui.lineStopCurrent->text().trimmed();
	QString lineStopVoltage = ui.lineStopVoltage->text().trimmed();

	int scale = g_project->deviceData(DeviceString)->scale(itemIndex);
	int volt_max = g_project->deviceData(DeviceString)->maxVoltage() * scale;
	int current_max = g_project->deviceData(DeviceString)->maxCurrent() * scale;

	step.clearParams();
	int stepType = m_typeMap[ui.boxType->currentIndex()];
	step.StepType = stepType;
	
	if (isVaildParam(stepType, PARAM_Time))
	{
		if (ui.boxSetTime->isChecked())
		{
			int ms = DataTrans::toTimeUnit(ui.lineTime->text());
			step.setTime(OP_BIG_EQU, ms);
		}
	}

	if (isVaildParam(stepType, PARAM_Voltage))
	{
		double volt;
		if (!checkData(tr("电压"), lineVolt, volt_max, volt))
			return false;

		step.setVoltage(OP_SMALL_EQU, volt);
	}
	if (isVaildParam(stepType, PARAM_Current))
	{
		double current;
		if (!checkData(tr("电流"), lineCurrent, current_max, current))
			return false;

		step.setCurrent(OP_SMALL_EQU, current);
	}
	if (isVaildParam(stepType, PARAM_StopCurrent))
	{
		double current;
		if (!checkData(tr("截止电流"), lineStopCurrent, current_max, current))
			return false;

		step.setStopCurrent(OP_BIG_EQU, current);
	}
	if (isVaildParam(stepType, PARAM_StopVoltage))
	{
		double voltage;
		if (!checkData(tr("截止电压"), lineStopVoltage, volt_max, voltage))
			return false;

		step.setStopVoltage(OP_BIG_EQU, voltage);
	}
	if (isVaildParam(stepType, PARAM_BeginStepID))
	{
		step.setBeginStepID(OP_EQU, ui.lineStartStep->value());
		step.setCountLoop(OP_EQU, ui.lineLoopTime->value());
	}

	if (step.StepType == STEP_TYPE_7)
	{		
		//截止电流的值必须小于电流值
		if(lineStopCurrent.toDouble() > lineCurrent.toDouble())
		{
			QMessageBox::information(this, "", tr("截止电流必须小于电流值"));
			return false;
		}
		//另外截止电流不能小于0.1
		if (lineStopCurrent.toDouble() < 0.1)
		{
			QMessageBox::information(this, "", tr("截止电流不能小于0.1"));
			return false;
		}
	}

	//额外处理
	if (step.StepType != STEP_TYPE_STOP)
	{
		step.setReservedParam1(OP_EQU, 0x41);
	}	

	return true;
}

void StepEditDialog::on_btnOk_clicked()
{
	if (!uiToStep())
		return;			

	accept();
}

void StepEditDialog::on_btnCancel_clicked()
{
	reject();
}

void StepEditDialog::on_boxType_currentIndexChanged(int index)
{
	for (auto w : m_paramWidgets)
		w->setVisible(false);

	int stepType = m_typeMap[ui.boxType->currentIndex()];
	ui.boxSetTime->setEnabled(true);

	//电流
	if (stepType == STEP_TYPE_1
		|| stepType == STEP_TYPE_2
		|| stepType == STEP_TYPE_7)
	{
		ui.label3->setVisible(true);
		ui.lineCurrent->setVisible(true);
	}
	//电压
	if (stepType == STEP_TYPE_3
		|| stepType == STEP_TYPE_7)
	{
		ui.label2->setVisible(true);
		ui.lineVolt->setVisible(true);
	}
	//截止电流
	if (stepType == STEP_TYPE_3
		|| stepType == STEP_TYPE_7)
	{
		ui.label4->setVisible(true);
		ui.lineStopCurrent->setVisible(true);
	}
	//截至电压
	if(stepType == STEP_TYPE_1
		|| stepType == STEP_TYPE_2)
	{
		ui.label5->setVisible(true);
		ui.lineStopVoltage->setVisible(true);
	}
	//时间
	if (stepType == STEP_TYPE_1
		|| stepType == STEP_TYPE_2
		|| stepType == STEP_TYPE_3
		|| stepType == STEP_TYPE_IDLE
		|| stepType == STEP_TYPE_7)
	{
		if (stepType == STEP_TYPE_IDLE)
		{
			ui.boxSetTime->setChecked(true);
			ui.boxSetTime->setEnabled(false);
		}
		else
			ui.boxSetTime->setEnabled(true);

		ui.boxSetTime->setVisible(true);
		ui.lineTime->setVisible(true);
	}
	//起始工步，循环次数
	if (stepType == STEP_TYPE_LOOP)
	{
		ui.label6->setVisible(true);
		ui.label7->setVisible(true);
		ui.lineStartStep->setVisible(true);
		ui.lineLoopTime->setVisible(true);

		ui.lineStartStep->setRange(1, step.StepLine - 1);
		ui.lineStartStep->setValue(step.BeginStepID());
		ui.lineLoopTime->setValue(step.CountLoop());		
	}
}